﻿using Liang.DataBase.Model.Entity;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Text;

namespace Liang.DataBase.Mapping.MySql
{
    public class UserMap : EntityTypeConfiguration<sys_user>
    {
        public UserMap()
        {
            // 表名
            this.ToTable("sys_user");

            // 主键
            this.HasKey(t => t.userId);

            // 属性设置
            this.Property(t => t.userId)
                .HasColumnName("userId")
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

            this.Property(t => t.name)
                .HasColumnName("name")
                .HasMaxLength(40)
                .IsRequired();

            this.Property(t => t.password)
                .HasColumnName("password")
                .HasMaxLength(40);

            this.Property(t => t.organId)
                .HasColumnName("organId");

            this.Property(t => t.sex)
                .HasColumnName("sex")
                .HasMaxLength(8)
                .IsRequired();

            this.Property(t => t.qq)
                .HasColumnName("qq")
                .HasMaxLength(20);

            this.Property(t => t.wx)
                .HasColumnName("wx")
                .HasMaxLength(20);

            // 关联设置
            this.HasRequired(t => t.organ)
                .WithMany(t => t.users)
                .HasForeignKey(t => t.organId);

            this.HasMany(t => t.roles)
                .WithMany()
                .Map(m =>
                {
                    m.MapLeftKey("userId");
                    m.MapRightKey("roleId");
                    m.ToTable("sys_user_role");
                });
        }
    }
}
